﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using Mengsk.ProxyDelivery.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Config
{
    /// <summary>
    /// Interaction logic for SystemConfigUserControl.xaml
    /// </summary>
    public partial class SystemConfigUserControl : UserControl, IPlugin
    {
        private const string CLOSEABLE_SHOES_SIZE = "CloseableShoesSize";
        private const string CLOSEABLE_SHOES_TYPE = "CloseableShoesType";
        private const string RETURN_WITH_NO_MONEY_LEVEL = "RETURN_WITH_NO_MONEY_LEVEL";
        private const string RETURN_WITH_NO_ORDER_MONEY_LESSDOWN = "RETURN_WITH_NO_ORDER_MONEY_LESSDOWN";
        private const string RETURN_WITH_NO_ORDER_MONEY_OVERUP = "RETURN_WITH_NO_ORDER_MONEY_OVERUP";
        private SystemConfigService systemConfigService = ServiceContainer.GetService<SystemConfigService>();
        private bool myLoaded = false;
        private SelectedValue[] closeableSize = null;
        private SelectedValue[] closeableType = null;

        public string Title
        {
            get { return "系统配置"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        public SystemConfigUserControl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.myLoaded)
                return;
            try
            {
                List<string> sizes = new List<string>();
                for (int i = 30; i < 50; i++)
                {
                    sizes.Add(i.ToString());
                }

                closeableSize = sizes.Select(obj => new SelectedValue { Selected = false, Value = obj, Title = obj }).ToArray();
                var s = systemConfigService.GetByField("name", CLOSEABLE_SHOES_SIZE);
                if (s != null && string.IsNullOrWhiteSpace(s.Value) == false)
                {
                    foreach (var item in closeableSize)
                    {
                        if (s.Value.Contains(item.Value))
                        {
                            item.Selected = true;
                        }
                    }
                }
                this.lst1.ItemsSource = closeableSize;

                closeableType = Enum.GetValues(typeof(ShoesType)).OfType<ShoesType>().Select(obj => new SelectedValue { Selected = false, Value = ((int)obj).ToString(), Title = EnumUtil.GetEnumValueDescription(obj) }).ToArray();
                s = systemConfigService.GetByField("name", CLOSEABLE_SHOES_TYPE);
                if (s != null && string.IsNullOrWhiteSpace(s.Value) == false)
                {
                    foreach (var item in closeableType)
                    {
                        if (s.Value.Contains(item.Value))
                        {
                            item.Selected = true;
                        }
                    }
                }
                this.lst2.ItemsSource = closeableType;

                s = this.systemConfigService.GetByField("name", RETURN_WITH_NO_MONEY_LEVEL);
                if (s != null && string.IsNullOrWhiteSpace(s.Value))
                {
                    this.tbReturnWithNoOrderMoney.Text = s.Value;
                }
                else
                {
                    this.tbReturnWithNoOrderMoney.Text = "100";
                }

                s = this.systemConfigService.GetByField("name", RETURN_WITH_NO_ORDER_MONEY_LESSDOWN);
                if (s != null && string.IsNullOrWhiteSpace(s.Value) == false)
                {
                    this.tbReturnWithNoOrderMoneyDown.Text = s.Value;
                }
                else
                {
                    this.tbReturnWithNoOrderMoneyDown.Text = "3";
                }

                s = this.systemConfigService.GetByField("name", RETURN_WITH_NO_ORDER_MONEY_OVERUP);
                if (s != null && string.IsNullOrWhiteSpace(s.Value) == false)
                {
                    this.tbReturnWithNoOrderMoneyOverUp.Text = s.Value;
                }
                else
                {
                    this.tbReturnWithNoOrderMoneyOverUp.Text = "5";
                }

                this.myLoaded = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string sizes = string.Join(",", this.closeableSize.Where(obj => obj.Selected).Select(obj => obj.Value));
                this.systemConfigService.SaveOrUpdate(CLOSEABLE_SHOES_SIZE, sizes);

                string types = string.Join(",", this.closeableType.Where(obj => obj.Selected).Select(obj => obj.Value));
                this.systemConfigService.SaveOrUpdate(CLOSEABLE_SHOES_TYPE, types);

                float f = float.Parse(this.tbReturnWithNoOrderMoneyDown.Text.Trim());
                this.systemConfigService.SaveOrUpdate(RETURN_WITH_NO_ORDER_MONEY_LESSDOWN, f.ToString("F2"));

                f = float.Parse(this.tbReturnWithNoOrderMoneyOverUp.Text.Trim());
                this.systemConfigService.SaveOrUpdate(RETURN_WITH_NO_ORDER_MONEY_OVERUP, f.ToString("F2"));

                f = float.Parse(this.tbReturnWithNoOrderMoney.Text.Trim());
                this.systemConfigService.SaveOrUpdate(RETURN_WITH_NO_MONEY_LEVEL, f.ToString("F2"));

                MessageBox.Show("已成功保存");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }


    class SelectedValue
    {
        public bool Selected { get; set; }

        public string Title { get; set; }

        public string Value { get; set; }
    }
}
